package com.yuanye.gwcit.widget;

/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.view.MotionEvent;
import android.view.View;

import com.google.mlkit.vision.barcode.common.Barcode;

public class QrCodeViewModel {
    private Rect boundingRect;
    private String qrContent;
    private View.OnTouchListener qrCodeTouchCallback;

    public QrCodeViewModel(Barcode barcode) {

        this.boundingRect = barcode.getBoundingBox();
        this.qrContent = "";
        this.qrCodeTouchCallback = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent e) {
                return false; // no-op
            }
        };

        switch (barcode.getValueType()) {
            case Barcode.TYPE_URL:
                this.qrContent = barcode.getUrl().getUrl();
                this.qrCodeTouchCallback = new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent e) {
                        if (e.getAction() == MotionEvent.ACTION_DOWN && boundingRect.contains((int)e.getX(), (int)e.getY())) {
                            Intent openBrowserIntent = new Intent(Intent.ACTION_VIEW);
                            openBrowserIntent.setData(Uri.parse(qrContent));
                            v.getContext().startActivity(openBrowserIntent);
                        }
                        return true; // return true from the callback to signify the event was handled
                    }
                };
                break;
            // Add other QR Code types here to handle other types of data,
            // like Wifi credentials.
            default:
//                this.qrContent = "Unsupported data type: " + barcode.getRawValue();
                this.qrContent = barcode.getRawValue();
                break;
        }
    }

    public Rect getBoundingRect() {
        return boundingRect;
    }

    public void setFlip(){
        this.boundingRect = new Rect(
                960-getBoundingRect().left,
                getBoundingRect().top,
                960-getBoundingRect().right,
                getBoundingRect().bottom
        );
    }

    public String getQrContent() {
        return qrContent;
    }

    public View.OnTouchListener getQrCodeTouchCallback() {
        return qrCodeTouchCallback;
    }
}
